﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WMS.Api.Application.Commands.Users;
using WMS.Common;
using WMS.Domain;

namespace WMS.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<UserController> _logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            this.mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="request">用户信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUser(AddUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加用户时发生错误");
                return new ApiResult<int>("添加用户")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"添加用户失败：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="request">用户信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateUser(UpdUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新用户时发生错误");
                return new ApiResult<int>("更新用户")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"更新用户失败：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>用户信息</returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<User>> GetUser(int id, CancellationToken cancellationToken)
        {
            try
            {
                var command = new GetUserCommand { Id = id };
                return await mediator.Send(command, cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户时发生错误");
                return new ApiResult<User>("获取用户")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"获取用户失败：{ex.Message}",
                    data = null
                };
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DeleteUser(int id, CancellationToken cancellationToken)
        {
            try
            {
                var command = new DelUserCommand { Id = id };
                return await mediator.Send(command, cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除用户时发生错误");
                return new ApiResult<int>("删除用户")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"删除用户失败：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>用户列表</returns>
        [HttpGet("list")]
        public async Task<ApiResult<List<User>>> GetAllUsers(int pageIndex = 1, int pageSize = 10, CancellationToken cancellationToken = default)
        {
            try
            {
                var command = new GetAllUsersCommand
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };
                return await mediator.Send(command, cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有用户时发生错误");
                return new ApiResult<List<User>>("获取所有用户")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"获取所有用户失败：{ex.Message}",
                    data = null
                };
            }
        }

        /// <summary>
        /// 修改用户金额
        /// </summary>
        /// <param name="request">金额修改信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateUserBalance(UpdateUserBalanceCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改用户金额时发生错误");
                return new ApiResult<int>("修改用户金额")
                {
                    isSuc = false,
                    code = StatusCodeEnum.ParamError,
                    msg = $"修改用户金额失败：{ex.Message}",
                    data = 0
                };
            }
        }
    }
}
